package com.ftar.urgent.api;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.ftar.urgent.api.req.KnowledageCommonReq;
import com.ftar.urgent.api.resp.GetIndustryTreeResp;
import com.ftar.urgent.api.resp.CheckItemResp;
import com.ftar.urgent.http.HttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class KnowledgeApi {

    private static String TAG = KnowledgeApi.class.getSimpleName();

    private static final String HOST_URL = "http://211.103.169.202:8888";

    // 查询行业树
    private static final String INDUSTRY_URI = "/api/common/industry";

    // 根据类型查询项目
    private static final String QUERY_ITEM_BY_CATEGORY_URI = "/api/common/project";

    // 语音指令查询
    private static final String VOICE_URI = "/api/common/voice";

    // 语音指令查询
    private static final String ALL_ITEM_URI = "/api/common/all";

    /**
     * 查询行业树数据
     */
    public static List<GetIndustryTreeResp> getIndustryTree() throws IOException {
        String url = HOST_URL + INDUSTRY_URI;
        String resStr = HttpUtils.get(url);
        Gson gson = new Gson();
        Type jsonType = new TypeToken<KnowledgeListResp<GetIndustryTreeResp>>() {
        }.getType();
        KnowledgeListResp<GetIndustryTreeResp> resp = gson.fromJson(resStr, jsonType);
        if (resp == null || resp.getData() == null || !resp.isSuccessful()) {
            Log.e(TAG, String.format("getIndustryTree call fail,fail code is %s", resp == null ? "" : resp.getCode()));
            return new ArrayList<>();
        }
        return resp.getData();
    }

    /**
     * 根据类型查询项目
     */
    public static CheckItemResp getProjectsCategory(KnowledageCommonReq req) throws IOException {
        String url = HOST_URL + QUERY_ITEM_BY_CATEGORY_URI;
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), JSON.toJSONString(req));
        String resStr = HttpUtils.post(url, requestBody);
        Gson gson = new Gson();
        Type jsonType = new TypeToken<KnowledgeResp<CheckItemResp>>() {
        }.getType();
        KnowledgeResp<CheckItemResp> resp = gson.fromJson(resStr, jsonType);
        if (resp == null || !resp.isSuccessful()) {
            Log.e(TAG, String.format("getProjectsCategory call fail,fail code is %s", resp == null ? "" : resp.getCode()));
            return new CheckItemResp();
        }
        return resp.getData();
    }


    /**
     * 获取全检查项
     */
    public static CheckItemResp getAllItem(KnowledageCommonReq req) throws IOException {
        String url = HOST_URL + ALL_ITEM_URI;
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), JSON.toJSONString(req));
        String resStr = HttpUtils.post(url, requestBody);
        Gson gson = new Gson();
        Type jsonType = new TypeToken<KnowledgeResp<CheckItemResp>>() {
        }.getType();
        KnowledgeResp<CheckItemResp> resp = gson.fromJson(resStr, jsonType);
        if (resp == null || !resp.isSuccessful()) {
            Log.e(TAG, String.format("getAllItem call fail,fail code is %s", resp == null ? "" : resp.getCode()));
            return new CheckItemResp();
        }
        return resp.getData();
    }


    /**
     * 语音指令查询
     */
    public static CheckItemResp queryVoice(KnowledageCommonReq req) throws IOException {
        String url = HOST_URL + VOICE_URI;
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), JSON.toJSONString(req));
        String resStr = HttpUtils.post(url, requestBody);
        Gson gson = new Gson();
        Type jsonType = new TypeToken<KnowledgeResp<CheckItemResp>>() {
        }.getType();
        KnowledgeResp<CheckItemResp> resp = gson.fromJson(resStr, jsonType);
        if (resp == null || !resp.isSuccessful()) {
            Log.e(TAG, String.format("queryVoice call fail,fail code is %s", resp == null ? "" : resp.getCode()));
            return new CheckItemResp();
        }
        return resp.getData();
    }

}
